<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>设置后台人员权限</title>
    <script src="/jquery/jquery-3.2.1.min.js"></script>
    <script src="/layui/layui.js"></script>
    <link rel="stylesheet" href="/layui/css/layui.css"/>
</head>
<body>
<form class="layui-form" lay-filter="form">
    <div style="margin-top: 10px;" class="layui-form-item" pane="">
        <label class="layui-form-label">人员名称</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" width="750px" id="trueName" disabled="disabled">
        </div>
    </div>
    <div style="margin-top: 15px;margin-left: 40px" class="layui-form-item" pane="">
        <p>权限设置：</p>
        <div class="layui-input-block" id="checkbox">
            <input type="checkbox" id="administration" name="administration" lay-skin="primary" title="管理">
            <input type="checkbox" id="teacher" name="teacher" lay-skin="primary" title="教师">
            <input type="checkbox" id="operate" name="operate" lay-skin="primary" title="运营">
            <input type="checkbox" id="personnelMatters" name="personnelMatters" lay-skin="primary" title="人事">
        </div>
    </div>

    <div style="margin-top: 15px;margin-left: 40px">
        <p>用户描述:</p>
        <textarea class="layui-textarea" style="width: 720px" id="remark" disabled="disabled"></textarea>
    </div>


    <div style="float: right; margin-top: 20px;margin-bottom: 10px;margin-right: 38px">
        <button type="button" lay-submit lay-filter="sub" class="layui-btn">确定</button>
        <button type="button" onclick="cancel()" class="layui-btn layui-btn-warm">取消</button>
    </div>
</form>
</body>
<script>

    layui.use(['layer','jquery','form'],function () {

        var layer = layui.layer,
            form = layui.form,
            $ = layui.$;

        var userId = parent.$('#userId').val();


        //获取用户拥有的权限
        $.ajax({
             url: '/jurisdiction/getAuthority'
            ,data: {'userId': userId}
            ,dataType: 'json'
            ,success:function(data){
                var arr = data.data;
                if(arr.length > 0){
                    $('#trueName').val(arr[0].trueName);
                    $('#remark').val(arr[0].remark);
                    for(var i=0; i<arr.length; i++){
                        if(arr[i].authorityId == 1){
                            $('#administration').prop("checked",true);
                        }else if(arr[i].authorityId == 2){
                            $('#teacher').prop("checked",true);
                        }else if(arr[i].authorityId == 3){
                            $('#operate').prop("checked",true);
                        }else if(arr[i].authorityId == 4){
                            $('#personnelMatters').prop("checked",true);
                        }
                        form.render('checkbox','form');
                    }
                }else{
                    $.ajax({
                         url: '/jurisdiction/getUserById'
                        ,data: {'userId': userId}
                        ,dataType: 'json'
                        ,success:function(data){
                             var user = data.data;
                            $('#trueName').val(user.trueName);
                            $('#remark').val(user.remark);
                        }
                    });
                }


            }

        });


        //确定
        form.on('submit(sub)',function(obj){

            var ua = obj.field;
            var authorityId;
            var arr = [];
            if(ua.administration == 'on'){
                arr.push(1);
            }
            if(ua.teacher == 'on'){
                arr.push(2);
            }
            if(ua.operate == 'on'){
                arr.push(3);
            }
            if(ua.personnelMatters == 'on'){
                arr.push(4);
            }
            $.ajax({
                 url: '/jurisdiction/delAuthority'
                ,data: {'userId': userId}
                ,dataType: 'json'
                ,success:function (data) {
                    for(var i=0; i<arr.length; i++){
                        $.ajax({
                            url: '/jurisdiction/editAuthority'
                            , data: {
                                'userId': userId
                                ,'authorityId': arr[i]
                            }
                            , dataType: 'json'
                            , success: function (data) {

                            }
                        });
                    }
                    layer.msg('修改成功');
                }
            });



            var layer = parent.layer;
            layer.close(layer.index);


        });


        cancel = function cancel(){
            var layer = parent.layer;
            layer.close(layer.index);
        }


    });

</script>
</html>